package com.ichifinga.airwave.shared;

import com.ichifinga.airwave.shared.entities.Phrase;
import com.ichifinga.airwave.shared.entities.PhraseLibrary;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class PhraseListActivity extends ListActivity implements OnItemLongClickListener {

	private void PopulateList()
	{
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        
        SharedPreferences sharedPreferences;
    	sharedPreferences = getSharedPreferences("SharedPreferences", Context.MODE_WORLD_READABLE);
        
    	int counter = 0;
    	
    	aa.add("? ? ? HELP ? ? ?");
    	
        for( counter = 1; counter <= PhraseLibrary.getInstance().getPhraseLimit(); counter ++)
        {
        	aa.add(sharedPreferences.getString("TextToDisplay" + counter, "Not setup"));
        };
                
        setListAdapter(aa);
        
	}
	
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {    	
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
         
        setContentView(R.layout.phraselistlayout);
        
        PopulateList();
        
        ListView lv = (ListView)findViewById(android.R.id.list);
        
        lv.setOnItemLongClickListener(this);
	}


    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
    	if(position == 0)
    	{
			Intent intent = new Intent(this, HelpActivity.class);
	    	startActivity(intent);
    	}
    	else
    	{
	    	setResult(position);
	    	finish();
    	}
    }


	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3)
	{
		if(position == 0)
		{
			Intent intent = new Intent(this, HelpActivity.class);
	    	startActivity(intent);
			return true;
		}
		else
		{
			Intent intent = new Intent(this, EditPhraseActivity.class);
	    	intent.putExtra("PhraseId", position);
	    	startActivityForResult(intent, 0);
	    	return true;
		}
	}

	@Override
	public void onActivityResult(int requestCode , int resultCode, Intent intent)
	{
		if(resultCode != RESULT_CANCELED)
		{
			PopulateList();
		}
	}
}
